package com.smfx.yxt;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.smfx.yxt.common.Const;
import com.smfx.yxt.daoSqlserver.OrganizeGroupMapper;
import com.smfx.yxt.daoSqlserver.TeacherInfoMapper;
import com.smfx.yxt.model.OrganizeGroup;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.model.TeacherOrganizeGroup;
import com.smfx.yxt.view.TermView;
import com.smfx.yxt.wechat.utils.WechatGlobalMap;

public class TeacherService extends BaseTest {

	@Autowired
	private OrganizeGroupMapper organizeGroupMapper;
	@Autowired
	private TeacherInfoMapper teacherInfoMapper;
	
	@Test
	public void teacher() {
		try {
			List<OrganizeGroup> list = organizeGroupMapper.selectListByParam(new OrganizeGroup());
			List<TeacherOrganizeGroup> departList = new ArrayList<>();
			if(list != null && list.size() > 0) {
				for(OrganizeGroup menu : list) {
					String key = null;
					Integer parentId = menu.getParentId();
					if(!(parentId != null && parentId != 0)) {
						key = "root";
					}else {
						key = String.valueOf(parentId);
					}
					TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
					if(key.equals("root")) {
						teacherOrganize.setGroupId(menu.getId());
						teacherOrganize.setGroupName(menu.getGroupName());
						teacherOrganize.setParentGroupId(menu.getParentId());
						teacherOrganize.setIds(menu.getIds());
						departList.add(teacherOrganize);
					}else {
						teacherOrganize.setGroupId(menu.getId());
						teacherOrganize.setGroupName(menu.getGroupName());
						teacherOrganize.setParentGroupId(menu.getParentId());
						teacherOrganize.setIds(menu.getIds());
						teacherOrganize.setGroupCode(menu.getGroupCode());
						for(TeacherOrganizeGroup parent : departList) {
							if(parentId.intValue() == parent.getGroupId().intValue()) {
								parent.setHasChild(true);
								parent.getSubList().add(teacherOrganize);
							}
						}
					}
				}
				List<TeacherInfo> teacherList = teacherInfoMapper.selectAll(1);
				if(teacherList != null && teacherList.size() > 0) {
					TermView view = (TermView) WechatGlobalMap.get(Const.CACHE_TERM).getValue();
					for(TeacherInfo teacher : teacherList) {
						TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
						teacherOrganize.setTeacherId(teacher.getId());
						teacherOrganize.setTeacherName(teacher.getName());
						for(TeacherOrganizeGroup group : departList) {
							if(isExistsPosition(group.getIds(), teacher.getPosition())) {
								if(!group.isHasChild()) {
									teacherOrganize.setGroupId(group.getGroupId());
									teacherOrganize.setParentGroupId(group.getParentGroupId());
									group.getSubList().add(teacherOrganize);
								}else {
									for(TeacherOrganizeGroup secondGroup : group.getSubList()) {
										if(secondGroup.getGroupCode().equals("SUBJECT_GROUP") && 
												isExistsPosition(secondGroup.getIds(), String.valueOf(teacher.getSubjectId()))) {
												teacherOrganize.setGroupId(secondGroup.getGroupId());
												teacherOrganize.setParentGroupId(secondGroup.getParentGroupId());
												secondGroup.getSubList().add(teacherOrganize);
											}else if(secondGroup.getGroupCode().equals("GRADE_GROUP")) {
												int ids = (view.getTermKey() - teacher.getGrade()) + (1 == teacher.getStudyType() ?  1 : 4);
												if(ids == Integer.valueOf(secondGroup.getIds()).intValue()) {
													teacherOrganize.setGroupId(secondGroup.getGroupId());
													teacherOrganize.setParentGroupId(secondGroup.getParentGroupId());
													secondGroup.getSubList().add(teacherOrganize);
												}
											}
									}
								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static boolean isExistsPosition(String p1, String p2) {
		if(StringUtils.isNotBlank(p1) && StringUtils.isNotBlank(p2)) {
			String[] array = p1.split(",");
			for(String val : array) {
				if(("," + p2 + ",").indexOf("," + val + ",") != -1) {
					return true;
				}
			}
		}
		return false;
	}
}
